<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/14
 * Time: 16:44
 * banner图
 */

namespace app\admin\controller;


use think\Request;

class Banner extends Base
{
    private $obj;
    public function _initialize()
    {
        $this -> obj = model('Banner');
    }

    public function index()
    {
        //获取banner图的信息
        $banners = $this -> obj -> getBannerInfo();
        //var_dump($banners);
        return $this -> fetch('',[
            'banners'=> $banners
        ]);
    }

    public function add()
    {
        return $this -> fetch();
    }

    public function save()
    {
        //var_dump(input('post.'));
        $data = input('post.');
        $validate = validate('Banner');
        if(!$validate -> scene('add') -> check($data)){
            $this -> error($validate -> getError());
        }
        if(!empty($data['id'])) {
            return $this->update($data);
        }
        //把相关信息传递到model层
        $res = $this->obj->add($data);
        if ($res){
            $this -> success('添加成功');
        }else{
            $this -> error('添加失败');
        }
    }

    //修改banner页面
    public function edit($id=0)
    {
        if (intval($id) <1){
            $this -> error('参数错误');
        }
        $banner = $this->obj->get($id);

        return $this -> fetch('',[
            'banner' => $banner
        ]);
    }

    //修改方法
    public function update($data)
    {
        $res = $this -> obj ->allowField(true)-> save($data,['id' => intval($data['id'])]);
        if($res) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }


}